package com.study;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.study.comp.HelloAop;
import com.study.comp.IHello;
import com.study.config.RootConfig;

public class AopSample {
	
	final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static void start(){
		
		ApplicationContext ap = new AnnotationConfigApplicationContext(RootConfig.class);
		IHello hello = (IHello) ap.getBean("iHello");
		hello.doHello();
		
		if(true)return;
		
		/**
		 * ProxyFactoryBean 직접 테스트 하기.
		 */
		
		@SuppressWarnings("unused")	ProxyFactoryBean pfBean = new ProxyFactoryBean();
		
		NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
		pointcut.setMappedName("do*");
		
		pfBean.setTarget(new HelloAop());
		//advice만 적용.
		//pfBean.addAdvice(new MyAdvice());
		
		//advisor = pointcut + advice
		pfBean.addAdvisor(new DefaultPointcutAdvisor(pointcut, new MyAdvice()));
		
		IHello iHello = (IHello) pfBean.getObject();
		iHello.doHello();
	}
	
	public static class MyAdvice implements MethodInterceptor{
		
		final Logger logger = LoggerFactory.getLogger(this.getClass());
		
		public Object invoke(MethodInvocation invocation) throws Throwable {
			logger.debug("this is a proxy class!!!");
			invocation.proceed();
			return null;
		}
	}
}
